package pers.mor.tomcat.http;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;

public class CompressionConfig {

    private String compression = "";
    private int compressionMinSize = 2048;
    private String noCompressionUserAgents = "";
    private String compressableMimeType = "";

    private Boolean browserSupport;
    private Boolean useCompression;

    public CompressionConfig() {
    }

    public CompressionConfig(Boolean browserSupport) {
        this.browserSupport = browserSupport;
    }

    public void checkIfBrowserIsSupportive(Request request) {

        String acceptEncoding = request.getHeader("Accept-Encoding");
        acceptEncoding = acceptEncoding == null ? "" : acceptEncoding;
        if (!acceptEncoding.contains("gzip")) {
            this.browserSupport = false;
            return;
        }

        String userAgents = this.noCompressionUserAgents;
        String[] userAgentArray = userAgents.split(",");
        for (String userAgent : userAgentArray) {
            String browserUserAgent = request.getHeader("User-Agent");
            browserUserAgent = browserUserAgent == null ? "" : browserUserAgent;
            if (StrUtil.containsAny(browserUserAgent, userAgent.trim())){
                this.browserSupport = false;
                return;
            }
        }

        this.browserSupport = true;
    }

    public String useCompression( byte[] body, String mimeType, String responseHead) {

        if (this.browserSupport == false) {
            this.useCompression = false;
            return responseHead;
        }

        if (!this.compression.equals("on")) {
            this.useCompression = false;
            return responseHead;
        }

        if (body.length <= this.compressionMinSize) {
            this.useCompression = false;
            return responseHead;
        }

        if (mimeType == null || mimeType.trim().equals("")) {
            this.useCompression = false;
            return responseHead;
        }

        if (mimeType.contains(";")){
            mimeType = StrUtil.subBefore(mimeType, ";", false);
        }

        String[] mimeTypes = this.compressableMimeType.split(",");
        for (String eachMimeType : mimeTypes) {
            if (mimeType.equals(eachMimeType)) {
                this.useCompression = true;
                return Constant.response_head_200_gzip;
            }
        }

        this.useCompression = false;
        return responseHead;
    }

    public byte[] gzipCompression(byte[] body) {
        if (this.useCompression == false) {
            return body;
        } else {
            return ZipUtil.gzip(body);
        }
    }

    public void setCompression(String compression) {
        this.compression = compression;
    }

    public void setCompressionMinSize(int compressionMinSize) {
        this.compressionMinSize = compressionMinSize;
    }

    public void setNoCompressionUserAgents(String noCompressionUserAgents) {
        this.noCompressionUserAgents = noCompressionUserAgents;
    }

    public void setCompressableMimeType(String compressableMimeType) {
        this.compressableMimeType = compressableMimeType;
    }

    public String getCompression() {
        return compression;
    }

    public int getCompressionMinSize() {
        return compressionMinSize;
    }

    public String getNoCompressionUserAgents() {
        return noCompressionUserAgents;
    }

    public String getCompressableMimeType() {
        return compressableMimeType;
    }

    public boolean isBrowserSupport() {
        return this.browserSupport;
    }

}
